﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using ITOMMS.Business;

namespace ITOMMS.Web.Dispatch
{
    public partial class DispatchAdd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownListHelper.InitDropList<tbTroubleServiceClassEntity>(new tbTroubleServiceClassEntity().GetAll(), this.ddlServiceID, "ID", "ClassName");//绑定服务类别
                DropDownListHelper.InitDropList<tbDepartmentEntity>(new tbDepartmentEntity().GetAll(), ddlNextDepartment, "ID", "DepartmentName");//绑定部门
                DropDownListHelper.InitDropList(ddlTaskSource, typeof(PubEnums.TaskSource));//事件来源
                DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("RoleGroup=" + (int)PubEnums.RoleGroup.客户), ddlOwnerCompnayID, "ID", "CompanyName");//绑定客户
                DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("RoleGroup!=" + PubEnums.RoleGroup.客户.ToString("D")), ddlNextPerson, "ID", "RealName");//下一步处理人
                DropDownListHelper.InitDropList(ddlState, typeof(ITOMMS.Business.PubEnums.DispatchState));
                DropDownListHelper.InitDropList<tbCustEmployeeInfoEntity>(new tbCustEmployeeInfoEntity().GetAllByQuery("CompanyID=" + ddlOwnerCompnayID.SelectedValue.ToString()), this.ddlRequestPeople, "ID", "RealName");//绑定员工表
                this.ddlRequestPeople.Items.Add(new ListItem("其他", "-1"));



                //编辑
                if (Request.QueryString["id"] != null)
                {
                    string strId = Request.QueryString["id"].ToString();
                    int id = 0;
                    if (int.TryParse(strId, out id))
                    {
                        dataBind(id);

                        if (SessionHelper.UserRoleGroup == PubEnums.RoleGroup.技术主管.ToString())
                        {
                            ddlTaskSource.Enabled = false;
                            ddlOwnerCompnayID.Enabled = false;
                            ddlRequestPeople.Enabled = false;
                            txtExpectTime.Enabled = false;
                            ddlServiceID.Enabled = false;
                            txtTitle.Enabled = false;
                            ddlUrgencyDegree.Enabled = false;
                            txtDispatchDescription.Enabled = false;
                            ddlNextDepartment.Enabled = false;
                            ddlNextPerson.Enabled = false;
                            txtTransactComments.Enabled = false;
                        }

                    }
                }
                else
                {
                    lblNO.Text = CommonHelper.GetBillNo(PubEnums.BusinessType.PB, PubEnums.YearsType.FourLen, 3);//自动编号P200911071028128789
                    //第一次新增加时，状态为未处理,并且不能编辑
                    ddlState.SelectedValue = ((int)PubEnums.TroubleState.未处理).ToString();

                    //隐藏分派内容
                    tablechuli.Visible = false;
                }

            }
        }

        private void dataBind(int id)
        {
            Entity.tbDispatchEntity entity = new ITOMMS.Entity.tbDispatchEntity();
            entity = entity.GetById(id);
            if (entity != null)
            {
                ViewState["Entity"] = entity;
                lblNO.Text = entity.DispatchNO;
                txtTitle.Text = entity.TaskTitle;
                ddlServiceID.Items.FindByValue(entity.ServiceID.ToString()).Selected = true;
                ddlOwnerCompnayID.Items.FindByValue(entity.OwnerCompnayID.ToString()).Selected = true;

                DropDownListHelper.InitDropList<tbCustEmployeeInfoEntity>(new tbCustEmployeeInfoEntity().GetAllByQuery("CompanyID=" + ddlOwnerCompnayID.SelectedValue.ToString()), this.ddlRequestPeople, "ID", "RealName");//绑定员工表
                this.ddlRequestPeople.Items.Add(new ListItem("其他","-1"));

                ddlRequestPeople.Items.FindByValue(entity.RequestPeople.ToString()).Selected = true;
                ddlState.Items.FindByValue(entity.State.ToString()).Selected = true;
                ddlNextPerson.Items.FindByValue(entity.NextPerson.ToString()).Selected = true;
                ddlNextDepartment.Items.FindByValue(entity.NextDepartment.ToString()).Selected = true;
                txtDispatchDescription.Text = entity.DispatchDescription;
                txtExpectTime.Text = entity.ExpectTime.ToString();
                txtFeedback.Text = entity.Feedback;
                txtTransactComments.Text = entity.TransactComments;
                ddlUrgencyDegree.SelectedValue = entity.PriorityLevel.ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Entity.tbDispatchEntity entity = new ITOMMS.Entity.tbDispatchEntity();
                if (ViewState["Entity"] != null)
                {
                    entity = ViewState["Entity"] as tbDispatchEntity;
                }
                else
                {
                    entity.CreateBy = SessionHelper.UserID;
                    entity.CreateDate = DateTime.Now;
                }
                entity.DispatchNO = lblNO.Text;
                entity.TaskTitle = txtTitle.Text.Trim();
                entity.TaskSource = int.Parse(ddlTaskSource.SelectedValue);
                entity.ServiceID = int.Parse(this.ddlServiceID.SelectedValue);
                entity.OwnerCompnayID = int.Parse(this.ddlOwnerCompnayID.SelectedValue);

                entity.RequestPeople = int.Parse(this.ddlRequestPeople.SelectedValue);

                entity.State = int.Parse(this.ddlState.SelectedValue);
                entity.NextDepartment = int.Parse(this.ddlNextDepartment.SelectedValue);
                entity.PriorityLevel = int.Parse(ddlUrgencyDegree.SelectedValue);
                if (this.ddlNextPerson.SelectedValue != "")
                {
                    entity.NextPerson = int.Parse(this.ddlNextPerson.SelectedValue);
                }
                entity.DispatchDescription = this.txtDispatchDescription.Text;
                entity.Feedback = this.txtFeedback.Text;
                entity.TransactComments = this.txtTransactComments.Text;
                entity.ModifyBy = SessionHelper.UserID;
                entity.ModifyDate = DateTime.Now;

                //entity.AppendixID
                DateTime rs = new DateTime();
                if (DateTime.TryParse(txtExpectTime.Text.Trim(), out  rs))
                {
                    entity.ExpectTime = rs;
                }
                entity.SaveOrUpdate(entity);

                ViewState["Entity"] = entity;

                MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            }
            catch (Exception ex)
            {
                log4netHelper.error(ex);
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            }
        }

        protected void ddlOwnerCompnayID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlOwnerCompnayID.SelectedValue != "-1")
            {
                DropDownListHelper.InitDropList<tbCustEmployeeInfoEntity>(new tbCustEmployeeInfoEntity().GetAllByQuery("CompanyID=" + ddlOwnerCompnayID.SelectedValue.ToString()), this.ddlRequestPeople, "ID", "RealName");//绑定员工表
                this.ddlRequestPeople.Items.Add(new ListItem("其他", "-1"));
            }
        }


    }
}
